package com.woniu.cq.mallorder.service.impl;

import com.woniu.cq.mallorder.domain.Order;
import com.woniu.cq.mallorder.domain.Orderitem;
import com.woniu.cq.mallorder.domain.Product;
import com.woniu.cq.mallorder.mapper.OrderMapper;
import com.woniu.cq.mallorder.mapper.OrderitemMapper;
import com.woniu.cq.mallorder.mapper.ProductMapper;
import com.woniu.cq.mallorder.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author daniel
 * @since 2020-09-19
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderitemMapper orderitemMapper;

    @Autowired
    private OrderMapper orderMapper;
    /**
     * @description: 创建订单
     * @param:
     * @return: void
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createOrder(Integer userId) {

        Order order = new Order();
        Orderitem orderitem = new Orderitem();
        Set<Object> keys =  stringRedisTemplate.opsForHash().keys("userId:" + userId);
        double totalMoney = 0;
        for (Object k : keys){
            System.out.println(k+"------------------------------------");
            Integer productId = Integer.parseInt(k.toString().split(":")[1]);
            Integer productNumber = Integer.parseInt(stringRedisTemplate.opsForHash().get("userId:" + userId, k).toString());
            System.out.println(productNumber);
            Product product = productMapper.selectById(productId);

            double productPrice = product.getProductPrice();
            System.out.println(productPrice);
            totalMoney += productPrice * productNumber;
        }
        //新增订单到数据库
        order.setOrderStatus(0);
        LocalDateTime time = LocalDateTime.now();
        System.err.println(time);
        order.setOrderTime(time);
        order.setUserId(userId);
        order.setTotalMoney(totalMoney);
        orderMapper.insert(order);
        //获取自增主键
        Integer orderId = order.getOrderId();
        System.out.println(orderId+"------------------------------------------------");
        //循环新增订单详情
        for (Object k : keys){
            Integer productId = Integer.parseInt(k.toString().split(":")[1]);
            Integer productNumber = Integer.parseInt(stringRedisTemplate.opsForHash().get("userId:" + userId, k).toString());
            Product product = productMapper.selectById(productId);
            double productPrice = product.getProductPrice();

            orderitem.setTotalPrice(productPrice*productNumber);
            orderitem.setNumber(productNumber);
            orderitem.setOrderId(orderId);
            orderitem.setProductId(productId);
            orderitem.setProductPrice(productPrice);
            orderitemMapper.insert(orderitem);
        }
        return orderId;
        /*//新增订单到redis
        stringRedisTemplate.opsForHash().put("order:"+userId,"orderId:",orderId+"");
        stringRedisTemplate.opsForHash().put("order:"+userId,"orderStatus:","已支付");
        */

    }
    /** 
     * @description: 得到某用户的所有订单 
     * @param: userId 
     * @return: java.util.List<com.woniu.cq.mallorder.domain.Order> 
     */
    @Override
    public List<Order> myOrder(Integer userId) {
        List<Order> allOrders = orderMapper.getAllOrders(userId);
        return allOrders;
    }
    /** 
     * @description: 修改订单状态 
     * @param: orderId 
     * @return: void 
     */
    @Override
    public void changeStatus(Integer orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        Order order1 = getById(orderId);
        if (order1.getOrderStatus()==0){
            order.setOrderStatus(1);
        }else {
            order.setOrderStatus(0);
        }
        updateById(order);
    }
}
